<?php
class My_Validate_Phone extends Zend_Validate_Abstract
{
    const NO_MATCH  = 'phoneNoMatch';
    const INVALID   = 'valueInvalid';

    protected static $_filter = null;

    protected $_messageTemplates = array(
        self::NO_MATCH => 'Неверный формат телефонного номера',
        self::INVALID  => 'Значение должно быть массивом',
    );

    public function isValid($value)
    {
        if (!is_array($value)) {
            $this->_error(self::INVALID);
            return false;
        }

        $this->_setValue($value);

        $empty = true;
        foreach ($this->_value as $value) {
            if(!empty($value)) $empty = false;
        }

        if ($empty) return true;

        if (null === self::$_filter) {
            require_once 'Zend/Filter/Digits.php';
            self::$_filter = new Zend_Filter_Digits();
        }

        foreach ($this->_value as $key => $value) {
            if (!empty($value) && $value != self::$_filter->filter($value)) {
                $this->_error(self::NO_MATCH);
                return false;
            }
        }

        if(!isset($this->_value['areanum']) || strlen($this->_value['areanum']) < 1 || strlen($this->_value['areanum']) > 2) {
            $this->_error(self::NO_MATCH);
            return false;
        }

        if(!isset($this->_value['geonum']) || strlen($this->_value['geonum']) < 1  || strlen($this->_value['geonum']) > 4) {
            $this->_error(self::NO_MATCH);
            return false;
        }

        if(!isset($this->_value['localnum']) || strlen($this->_value['localnum']) < 1 || strlen($this->_value['localnum']) > 8) {
            $this->_error(self::NO_MATCH);
            return false;
        }

        if(isset($this->_value['extnum']) && strlen($this->_value['extnum']) > 4) {
            $this->_error(self::NO_MATCH);
            return false;
        }

        return true;
    }
}
